package com.imooc.course.business.controller.admin;

import com.imooc.course.business.form.SectionForm;
import com.imooc.course.server.dto.ResponseDto;
import com.imooc.course.server.dto.SectionDto;
import com.imooc.course.server.dto.SectionPageDto;
import com.imooc.course.server.service.SectionService;
import com.imooc.course.server.utils.CopyUtil;
import com.imooc.course.server.utils.ValidatorUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/admin/section")
public class SectionController {

    private static final Logger log = LoggerFactory.getLogger(SectionController.class);
    public static final String BUSINESS_NAME = "小节";


    @Resource(name = "sectionService")
    private SectionService sectionService;

    /**
     * 查询列表
     * @param sectionPageDto 传入的page和size
     * @return
     */
    @PostMapping("/list")
    public ResponseDto getList(@RequestBody SectionPageDto sectionPageDto) {
        ValidatorUtil.notNull(sectionPageDto.getChapterId(),"大章ID");
        ValidatorUtil.notNull(sectionPageDto.getCourseId(),"课程ID");
        sectionService.getList(sectionPageDto);
        return ResponseDto.success().setContent(sectionPageDto);
    }

    /**
     * 保存，id有值为更新，id无值为新增
     * @param sectionForm
     * @return
     */
    @PostMapping("/save")
    public ResponseDto addSection(@RequestBody SectionForm sectionForm) {
        //入参检查
        ValidatorUtil.notNull(sectionForm.getTitle(), "标题");
        ValidatorUtil.minimumToMaximum(sectionForm.getTitle(), "标题", 1, 50);
        ValidatorUtil.minimumToMaximum(sectionForm.getVideo(), "视频", 1, 200);

        SectionDto sectionDto = CopyUtil.copy(sectionForm, SectionDto.class);
        sectionService.save(sectionDto);
        return ResponseDto.success().setContent(sectionDto);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public ResponseDto del(@PathVariable String id) {
        sectionService.delete(id);
        return ResponseDto.success();
    }
}
